/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bdl;

import entities.Match;
import java.util.Date;
import java.util.List;
import javax.persistence.Query;
import javax.persistence.TemporalType;

/**
 *
 * @author olawale
 */
public class MatchBean {

    private static DataAccess da = DataAccess.getDataAccess();

    public static Match createMatch(Match match){
        try{
            da.em.getTransaction().begin();
            da.em.persist(match);
            da.em.getTransaction().commit();
            return match;
        }catch(Exception ex){
            System.out.println(ex.toString());
            return null;
        }
    }

    public static Match updateMatch(Match match){
        try{
            da.em.getTransaction().begin();
            Match m = da.em.merge(match);
            da.em.getTransaction().commit();
            return m;
        }catch(Exception ex){
            System.out.println(ex.toString());
            return null;
        }
    }

    public static Match getMatch(int id){
        Match match = da.em.find(Match.class, id);
        return match;
    }

    public static List<Match> getMatches(Date date, int fixture){
        Query query = da.em.createQuery("SELECT m FROM Match m WHERE m.matchDate = :d AND " +
                "m.fixture = :f AND m.awayScore = m.homeScore");

        query.setParameter("d", date, TemporalType.DATE);
        query.setParameter("f", fixture);

        try{
            List<Match> results = query.getResultList();
            return results;
        }catch(Exception ex){
            System.err.println(ex.toString());
        }

        return null;
    }

    public static List<Match> getMatches(Date startDate, Date endDate, int fixture){
        Query query = da.em.createQuery("SELECT m FROM Match m WHERE m.matchDate BETWEEN :sd AND :ed AND " +
                "m.fixture = :f AND m.awayScore = m.homeScore");

        query.setParameter("sd", startDate, TemporalType.DATE);
        query.setParameter("ed", endDate, TemporalType.DATE);
        query.setParameter("f", fixture);

        try{
            List<Match> results = query.getResultList();
            return results;
        }catch(Exception ex){
            System.err.println(ex.toString());
        }

        return null;
    }

    public static List<Match> getMatches(Date date){
        Query query = da.em.createQuery("SELECT m FROM Match m WHERE m.matchDate = :d AND " +
                "m.awayScore = m.homeScore");

        query.setParameter("d", date, TemporalType.DATE);

        try{
            List<Match> results = query.getResultList();
            return results;
        }catch(Exception ex){
            System.err.println(ex.toString());
        }

        return null;
    }

    public static List<Match> getAllMatches(Date date){
        Query query = da.em.createQuery("SELECT m FROM Match m WHERE m.matchDate = :d AND m.homeScore > -1" +
                "AND m.awayScore > -1");

        query.setParameter("d", date, TemporalType.DATE);

        try{
            List<Match> results = query.getResultList();
            return results;
        }catch(Exception ex){
            System.err.println(ex.toString());
        }

        return null;
    }

    public static void removeMatch(Match match){
        try{
            da.em.getTransaction().begin();
            da.em.remove(match);
            da.em.getTransaction().commit();
        }catch(Exception ex){
            System.out.println(ex.toString());
        }
    }

    public static List<Match> getAllMatches(){
        Query query = da.em.createQuery("SELECT m FROM Match m WHERE m.homeScore > -1 " +
                "AND m.awayScore > -1");

        try{
            List<Match> list = query.getResultList();
            return list;
        }catch(Exception ex){
            System.out.println(ex.toString());
        }
        return null;
    }

    public static List<Match> getPrevMatches(Match match, Date startDate, Date endDate){
        Query query = da.em.createNativeQuery("CALL GetPrevMatches(?, ?, ?, ?)", Match.class);

        query.setParameter(1, startDate, TemporalType.DATE);
        query.setParameter(2, endDate, TemporalType.DATE);
        query.setParameter(3, match.getAway().getId());
        query.setParameter(4, match.getHome().getId());
        query.setHint("toplink.pessimistic-lock", "Lock");

        try{
            List<Match> results = query.getResultList();
            return results;
        }catch(Exception ex){
            System.err.println(ex.toString());
        }

        return null;
    }

    public static void clearScores(Match match){
        Query query = da.em.createQuery("DELETE FROM Score S WHERE s.match = :m");
        query.setParameter("m", match);
        da.em.getTransaction().begin();
        query.executeUpdate();
        da.em.getTransaction().commit();
    }
}
